<script setup lang='ts'>
import type { OptionsType, PlusColumn } from 'plus-pro-components'

import { useAddSupplierOfProject } from '@fl/api/purchase'
import { useGetSupplierOptions } from '@fl/api/supplier'
import SubmitBtn from '@fl/components/submit-btn.vue'
import { treeFindLabel } from '@fl/utils/tree'
import { cloneDeep } from 'lodash-es'

const props = defineProps<{
    projectId: string
    projectName: string
}>()
const visible = defineModel<boolean>('visible', { required: true })

const initialState = {} as any
const state = ref(cloneDeep(initialState))

const dialogTitle = '新增供应商'

const getSupplierOptionsParams = computed<boolean>(() => unref(visible))

const { data: supplierOptionsData, isLoading: isGetSupplierOptionsLoading } = useGetSupplierOptions(getSupplierOptionsParams)
const { isPending: isAddSupplierOfProjectPending, mutate: addSupplierOfProject } = useAddSupplierOfProject()

// #region Form
const formRef = ref()
const isFormLoading = computed(() => isGetSupplierOptionsLoading.value)
const isSubmitLoading = computed(() => isAddSupplierOfProjectPending.value)

const columns: PlusColumn[] = [
    {
        fieldProps: {
            filterable: true,
        },
        label: '供应商名称',
        options: supplierOptionsData as OptionsType,
        prop: 'supplierId',
        valueType: 'select',
    },
]

const rules = {
    supplierId: [
        {
            message: '请选择供应商',
            required: true,
            trigger: 'change',
        },
    ],
}

function submit() {
    const params = {
        biddingProjectId: props.projectId,
        biddingProjectName: props.projectName,
        supplierId: state.value.supplierId,
        supplierName: treeFindLabel({ targetValue: state.value.supplierId, treeData: supplierOptionsData.value! }),
    }
    addSupplierOfProject(params, {
        onSuccess() {
            ElMessage.success('操作成功')
            cancel()
        },
    })
}
// #endregion

// #region UI Listener
function onSaveBtnClick() {
    formRef.value.handleSubmit()
}

function cancel() {
    visible.value = false
}

function closed() {
    state.value = cloneDeep(initialState)
    formRef.value.formInstance?.resetFields()
}
// #endregion
</script>

<template>
    <PlusDialog v-model="visible"
                :title="dialogTitle"
                close-on-press-escape
                @cancel="cancel"
                @closed="closed"
    >
        <el-skeleton v-if="isFormLoading"
                     animated
        />

        <PlusForm v-else
                  ref="formRef"
                  v-model="state"
                  :columns="columns"
                  :label-width="100"
                  :rules="rules"
                  :has-footer="false"
                  :row-props="{ gutter: 40 }"
                  label-position="top"
                  label-suffix=""
                  @submit="submit"
        />

        <template #footer>
            <div class="mx-auto">
                <SubmitBtn type="primary"
                           :loading="isSubmitLoading"
                           @click="onSaveBtnClick"
                >
                    保存
                </SubmitBtn>

                <el-button @click="cancel">
                    取消
                </el-button>
            </div>
        </template>
    </PlusDialog>
</template>
